<template>
  <div class="container">
    <!-- 左侧 -->
    <div class="left">
      <!-- 菜单选择组件 -->
      <MenuNav />
      <!-- 推荐城市模块 -->
      <div class="aside-recommend">
        <h4 class="aside-title">推荐城市</h4>
        <nuxt-link to="/post#" class="aside-recommend-item">
          <img src="@/assets/images/pic_sea.jpeg" />
        </nuxt-link>
      </div>
    </div>
    <div class="right">
      <!-- 搜索模块 -->
      <SearchCity v-if="postList" @searchPost="goSearchPost" />
      <!-- 推荐攻略 -->
      <!-- 头部文字 -->
      <div class="strategy">
        <h3>推荐攻略</h3>
        <el-button type="primary" icon="el-icon-edit" @click="createStory">写游记</el-button>
      </div>
      <!-- 文章列表组件 -->
      <PostItem :articlesData="item" v-for="(item,index) in dataList" :key="index" />
      <!-- 没有结果的显示 -->
      <div v-if="postList.length == 0" style="height:50px;color:#999;margin-top:20px">抱歉，没找到该城市的相关攻略</div>
      <!-- 分页结构 -->
      <el-pagination
        v-if="postList.length != 0"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageIndex"
        :page-sizes="[1, 3, 5, 10]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
import MenuNav from '@/components/post/menuNav.vue'
import SearchCity from '@/components/post/searchCity.vue'
import PostItem from '@/components/post/postItem.vue'
export default {
  components: {
    MenuNav, SearchCity, PostItem
  },
  data() {
    return {
      postList: [],
      pageIndex: 1,
      pageSize: 3,
      total: 0,        //文章总数目
      dataList: [],      //存放当前页的文章数据
      isShow: false      //没有搜索结果结构
    }
  },
  mounted() {
    this.getPostData()  //获取所有文章数据
  },
  methods: {
    // 封装 获取所有文章数据
    getPostData() {
      // console.log(this.$route.query.city);
      this.$axios({
        url: '/posts',
      })
        .then(res => {
          console.log(res);
          this.postList = res.data.data
          this.total = res.data.total
          //获取当前页
          this.getPageData()
        })
    },
    getPageData() {
      this.dataList = this.postList.slice(this.pageSize * (this.pageIndex - 1), this.pageSize * this.pageIndex)
      // console.log(this.dataList);
    },
    // 更改每页显示记录数 
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.pageSize = val
      this.getPageData()
    },
    // 更改当前显示页
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.pageIndex = val
      this.getPageData()
    },
    // 监听搜索组件的 信息
    goSearchPost(cn) {
      this.pageIndex = 1;
      console.log(cn);
      this.$axios({
        url: '/posts',
        params: { city: cn }
      })
        .then(res => {
          console.log(res);
          this.postList = res.data.data
          this.total = res.data.total
          //获取当前页
          this.getPageData()
        })
        .catch(err => {
          console.dir(err)
        })

      if (cn === '') {          //输入空值，渲染所有
        this.getPostData()
      }
    },
    //写游记
    createStory() {
      // console.log(this.$store.state.user.userinfo.token);
      if (this.$store.state.user.userinfo.token) {
        this.$router.push('/post/create')
      } else {
        this.$message.warning({
          showClose: true,
          message: '未登录，请先登录'
        })
        this.$router.push('/user/login')
        this.$store.commit('user/setBackurl', location.href)
      }

    }
  },
  watch: {
    $route() {
      if (!this.$route.query.city) {  //点击头部导航--攻略渲染所有
        this.getPostData()
      }
    }
  }
}
</script>

<style lang="less" scoped>
.container {
  display: flex;
  width: 1000px;
  margin: 20px auto;
  .left {
    flex: 25%;
    // background-color: pink;
    // height: 800px;
    margin-right: 40px;
    .aside-recommend {
      margin-top: 20px;
      .aside-title {
        font-weight: 400;
        padding-bottom: 10px;
        border-bottom: 1px solid #ddd;
        margin-bottom: 10px;
      }
      .aside-recommend-item img {
        width: 100%;
      }
    }
  }
  .right {
    flex: 75%;
    // width: 100%;
    // background-color: pink;
    .strategy {
      height: 50px;
      width: 100%;
      display: flex;
      justify-content: space-between;
      align-items: center;
      border-bottom: 1px solid #ddd;
      h3 {
        font-weight: 400;
        color: #ffa500;
        height: 50px;
        line-height: 50px;
        border-bottom: 2px solid #ffa500;
        font-size: 18px;
      }
    }
  }
}
</style>